<template>
	<el-dialog
		v-model="visible"
		:close-on-click-modal="false"
		:title="dailogType == 'add' ? '新增' : dailogType == 'edit' ? '修改' : '详情'"
		draggable
		:append-to-body="true"
		width="30%"
	>
		<el-form ref="dataFormRef" v-loading="loading" :model="form" :rules="dataRules" label-width="90px">
			<el-row :gutter="24">
				<el-col :span="24" class="mb20">
					<el-form-item label="所属产品:" prop="productKey">
						<el-select :disabled="dailogType == 'detail'" v-model="form.productKey" placeholder="请选择产品">
							<el-option v-for="item in props.productData" :key="item.id" :label="item.name" :value="item.productKey" />
						</el-select>
					</el-form-item>
				</el-col>
			</el-row>
			<el-row :gutter="24">
				<el-col :span="24" class="mb20">
					<el-form-item label="设备名称:" prop="deviceName">
						<el-input :disabled="dailogType == 'detail'" v-model="form.deviceName" placeholder="请输入设备名称" />
					</el-form-item>
				</el-col>
			</el-row>
			<el-row :gutter="24">
				<el-col :span="24" class="mb20">
					<el-form-item label="所属区域:" prop="subareaCode">
						
						<el-tree-select 
							:disabled="dailogType == 'detail'" filterable  
							:data="subareaData"
							:props="{ value: 'subareaCode', label: 'subareaCn', children: 'childrens' }" 
							v-model="form.subareaCode" 
							placeholder="请选择所属区域"
							>
						</el-tree-select>
					</el-form-item>
				</el-col>
			</el-row>
			<el-row :gutter="24">
				<el-col :span="24" class="mb20">
					<el-form-item label="备注:" prop="remark">
						<el-input :disabled="dailogType == 'detail'" v-model="form.remark" placeholder="请输入备注" />
					</el-form-item>
				</el-col>
			</el-row>
		</el-form>
		<template #footer>
			<span class="dialog-footer">
				<el-button @click="visible = false">{{ $t('common.cancelButtonText') }}</el-button>
				<el-button type="primary" @click="onSubmit" :disabled="loading">{{ $t('common.confirmButtonText') }}</el-button>
			</span>
		</template>
	</el-dialog>
</template>

<script lang="ts" name="deviceFormDailog" setup>
import { defineProps } from 'vue';
import { useMessage } from '/@/hooks/message';
import { addObj, editObj, getObj } from '/@/api/basiciot/device/device';
import { fetchList } from '/@/api/basiciot/area/fenqu';
import { useI18n } from 'vue-i18n';

// 定义子组件向父组件传值/事件
const emit = defineEmits(['refresh']);
// 定义父组件传过来的值
const props = defineProps({
	productData: {
		type: Array,
		default: () => [],
	},
});

const { t } = useI18n();
//弹窗类型
const dailogType = ref('add');
// 定义变量内容
const dataFormRef = ref();
const visible = ref(false);
const loading = ref(false);
const subareaData: any = ref([])
// 定义字典

// 提交表单数据
const form = reactive({
	id: '',
	productKey: '',
	deviceName: '',
	subareaCode: '',
	remark: '',
});
// 定义校验规则
const dataRules = ref({
	productKey: [{ required: true, message: '所属产品不能为空', trigger: 'blur' }],
});


onMounted(() => {
	getSubarea()
})

// 打开弹窗
const openDialog = (row: any, type: string) => {
	visible.value = true;
	dailogType.value = type;
	// 重置表单数据
	nextTick(() => {
		dataFormRef.value?.resetFields();
	});
	if (type === 'edit' || type === 'detail') {
		getData(row.unitNo);
	} else {
		form.id = '';
	}
};
const getData = (id: string) => {
	loading.value = true;
	getObj(id)
		.then((res) => {
			Object.assign(form, res.data);
		})
		.finally(() => {
			loading.value = false;
		});
};
// 提交
const onSubmit = async () => {
	const valid = await dataFormRef.value.validate().catch(() => {});
	if (!valid) return false;

	loading.value = true;

	try {
		if (form.id) {
			await editObj(form);
			useMessage().success(t('common.editSuccessText'));
		} else {
			await addObj(form);
			useMessage().success(t('common.addSuccessText'));
		}
		visible.value = false;
		emit('refresh');
	} catch (err: any) {
		useMessage().error(err.msg);
	} finally {
		loading.value = false;
	}
};
const getSubarea = () => {
	fetchList({currentPage: 1, pageSize: 9999}).then(res => {
		subareaData.value = res.data.records || []
	})
}
// 暴露变量
defineExpose({
	openDialog,
});
</script>
